<?php
App::uses('AppController', 'Controller');
/**
 * Groups Controller
 *
 * @property Group $Group
 */
class GroupsController extends AppController {

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Group->recursive = 0;
		$this->set('groups', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Group->exists($id)) {
			throw new NotFoundException(__('Invalid group'));
		}
		$options = array( 'conditions' => array('Group.' . $this->Group->primaryKey => $id));
		$this->set('group', $this->Group->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Group->create();
			$institution_id=Cache::read('institution_id');
			$this->request->data['Group']['institution_id']=$institution_id;
			if ($this->Group->save($this->request->data)) {
				Cache::delete('institution_id');
				$this->Session->setFlash(__('El grupo fué guardado con éxito'));
				$this->redirect(array('controller'=>'institutions','action' => 'view',$institution_id));
			} else {
				$this->Session->setFlash(__('El grupo no pudo ser guardo. Por favor, intentelo otra vez.'));
			}
		}
		$institutions = $this->Group->Institution->find('list');
		$this->set(compact('institutions'));
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Group->exists($id)) {
			throw new NotFoundException(__('Invalid group'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Group->save($this->request->data)) {
				$this->Session->setFlash(__('The group has been saved'));			
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The group could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Group.' . $this->Group->primaryKey => $id));
			$this->request->data = $this->Group->find('first', $options);
		}
		$institutions = $this->Group->Institution->find('list');
		$this->set(compact('institutions'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Group->id = $id;
		if (!$this->Group->exists()) {
			throw new NotFoundException(__('Invalid group'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Group->delete()) {
			$this->Session->setFlash(__('Grupo Eliminado'));
			//echo "<script>history.back(0);</script>";			
			$this->redirect($this->referer());
		}
		$this->Session->setFlash(__('El grupo no pudo ser eliminado'));
		$this->redirect($this->referer());
	}

	function addStudent($group_id,$institution_id)
	{
		Cache::write("group_id",$group_id);
		Cache::write("group_institution_id",$institution_id);
		$this->redirect("/group_users/add");
	}


	function changeLider($group_id,$user_id)
	{
		$this->Group->query("update group_users set lider=0 where group_id=$group_id and lider=1");
		$this->Group->query("update group_users set lider=1 where group_id=$group_id and user_id=$user_id");
		$this->redirect("/groups/view/".$group_id);
	}
}
